package com.hqhsc.module.manage.activity.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.common.dao.CommonDao;
import com.hqhsc.module.front.activity.domain.Vote;
import com.hqhsc.module.front.activity.domain.VoteRowMapper;
import com.hqhsc.module.manage.activity.domain.Activity;
import com.hqhsc.module.manage.activity.domain.ActivityRowMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class VoteManageDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(VoteManageDao.class);


    @Autowired
    private CommonDao commonDao;


    public int getVoteCount(long activity_id) {
        String sql = "select count(1) from votes ";
        Object[] o = new Object[]{ };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForInt(sql , o);
    }

    public List<Vote> getVoteList(long activity_id , int start ,  int size) {

        String sql = "select t.* from votes t where 1=1 ";
        List<Object> list = new ArrayList<Object>();

        if(activity_id != -1) {//-1表示查所有；0表示线下；1表示线上
            sql = sql + " and activity_id = ? ";
            list.add(activity_id);
        }
        sql = sql + " order by id desc limit ? ,? ";
        list.add(start);
        list.add(size);
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new VoteRowMapper());
    }

    /**
     * 审核作品
     * @param id
     * @return
     */
    public int productAuth(int result_status , long id , int origin_status) {
        String sql = "update votes set status = ? where id = ? and status = ?";
        Object[] o = new Object[]{ result_status , id , origin_status };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

}
